/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package de.swta;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

/**
 *
 * @author Jan Korreck
 */
public class MyItemManager {

    private static MyItemManager INSTANCE = new MyItemManager();

    private HashMap<String, MyItem> allItems = new HashMap<String, MyItem>();

    private ArrayList<PropertyChangeListener> l = new ArrayList<PropertyChangeListener>();

    public MyItemManager() {
        
    }

    public static MyItemManager getInstance() {
        return INSTANCE;
    }


    public void addItem(MyItem newItem) {
        if (!this.allItems.containsKey(newItem.getId())) {
            this.allItems.put(newItem.getId(), newItem);
        }
    }

    public void removeItem(MyItem newItem) {
        this.allItems.remove(newItem);
    }

    public void removeItem(String id) {
        if (allItems.containsKey(id)) {
            this.allItems.remove(id);
            this.l.get(0).propertyChange(new PropertyChangeEvent(this, "remove", id, ""));
        }
    }

    public MyItem getItem(String id) {
        return this.allItems.get(id);
    }

    public boolean hasItem(String itemID) {
        return this.allItems.containsKey(itemID);
    }

    public List<MyItem> getAllItems() {
        return new ArrayList(this.allItems.values());
    }

    public int getSize() {
        return this.allItems.size();
    }

    public List<MyItem> find(int first, int count, String sortProperty, boolean sortAsc) {

        List<MyItem> subList = getIndex(sortProperty, sortAsc).subList(first, first + count);

        return subList;
    }

    private List<MyItem> getIndex(String sortProperty, boolean sortAsc) {
        // hier müsste die DB gefragt werden und dementsprechend sortiert werden

       if (sortProperty == null) {
           return new ArrayList<MyItem>(this.allItems.values());

       } else if (sortProperty.equals("id")) {

           ArrayList<MyItem> tmpItem = new ArrayList<MyItem>(this.allItems.values());


           return this.sortByID(tmpItem, sortAsc);

       } else if (sortProperty.equals("name")) {

           return this.sortByName(new ArrayList<MyItem>(this.allItems.values()), sortAsc);

       } else if (sortProperty.equals("category")) {

           return this.sortByCategory(new ArrayList<MyItem>(this.allItems.values()), sortAsc);
       }

       throw new RuntimeException("unknown sort option [" + sortProperty +
                "]. valid options: [id] , [name], [category]");
    }


    private List<MyItem> sortByID(ArrayList<MyItem> items, boolean asc) {

        if (asc) {
            Collections.sort(items, new Comparator() {

                public int compare(Object o1, Object o2) {

                    return ((MyItem)o1).getId().compareTo(((MyItem)o2).getId());
                }
            });
        } else {
            Collections.sort(items, new Comparator() {

                public int compare(Object o1, Object o2) {

                    return ((MyItem)o2).getId().compareTo(((MyItem)o1).getId());
                }
            });
        }
        return items;
    }


    private List<MyItem> sortByName(ArrayList<MyItem> items, boolean asc) {

        if (asc) {
            Collections.sort(items, new Comparator() {

                public int compare(Object o1, Object o2) {

                    return ((MyItem)o1).getName().compareTo(((MyItem)o2).getName());
                }
            });
        } else {
            Collections.sort(items, new Comparator() {

                public int compare(Object o1, Object o2) {

                    return ((MyItem)o2).getName().compareTo(((MyItem)o1).getName());
                }
            });
        }
        return items;
    }

    private List<MyItem> sortByCategory(ArrayList<MyItem> items, boolean asc) {

        if (asc) {
            Collections.sort(items, new Comparator() {

                public int compare(Object o1, Object o2) {

                    return ((MyItem)o1).getCategory().compareTo(((MyItem)o2).getCategory());
                }
            });
        } else {
            Collections.sort(items, new Comparator() {

                public int compare(Object o1, Object o2) {

                    return ((MyItem)o2).getCategory().compareTo(((MyItem)o1).getCategory());
                }
            });
        }
        return items;
    }

public void addPCL(PropertyChangeListener l) {
    this.l.add(l);
}

}
